package com.ybb.stock.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ybb.stock.entity.GoodsStocksEntity;
import org.apache.ibatis.annotations.*;

@Mapper
public interface GoodsStocksMapper extends BaseMapper<GoodsStocksEntity> {

    /**
     * 获取库存量并锁住库存记录。
     */
    @Select("select num from ybb_goods_stocks where sku=#{sku} and merchant=#{merchatCode} for update")
    Integer getGoodsStockInLock(@Param("sku") String sku, @Param("merchatCode") String merchatCode);

    @Insert("insert into ybb_goods_stocks(sku, num, merchant) values(#{stock.sku}, #{stock.num}, #{stock.merchant})")
    Integer insertStocks(@Param("stock") GoodsStocksEntity stock);

    @Update("update ybb_goods_stocks set num=num+#{num} where sku=#{sku} and merchant=#{merchant}")
    Integer incrementStocks(@Param("sku") String sku, @Param("num") Integer num , @Param("merchant") String merchant);


}
